package com.bflow.impl;

import com.bflow.api.IBflowExtResService;
import com.bflow.api.bo.ext.BflowRoleBo;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.commons.bean.PageResult;
import com.bflow.commons.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 功能：IBflowExtResService 扩展资源接口
 * 作者：haowen
 * 日期：2018-12-18
 */
@Service("bflowExtResService")
public class BflowExtResServiceImpl implements IBflowExtResService {

	//临时测试方案
	public static Map<String, BflowUserBo> USER_LIST = new ConcurrentHashMap<>(32);
	private static Map<String, BflowRoleBo> ROLE_LIST = new ConcurrentHashMap<>();

	static {
		initUserList();
		initRoleList();
	}

	private static void initUserList(){
		USER_LIST.put("1001", new BflowUserBo("1001", "李志坚", "项目部-经理", "pm_manger", "15577777777"));
		USER_LIST.put("1002", new BflowUserBo("1002", "宋小龙", "项目部-经理", "pm_manger", "15588888888"));
		USER_LIST.put("1003", new BflowUserBo("1003", "张三", "项目部-客服", "pm_kefu", "15511111111"));
		USER_LIST.put("1004", new BflowUserBo("1004", "李四", "项目部-客服", "pm_kefu", "15522222222"));
		USER_LIST.put("1005", new BflowUserBo("1005", "马六", "项目部-客服", "pm_kefu", "15533333333"));

		USER_LIST.put("1011", new BflowUserBo("1011", "陈协洲", "拓展部-经理", "ext_manger", "15599999999"));
		USER_LIST.put("1012", new BflowUserBo("1012", "盘江玲", "拓展部-经理", "ext_manger", "15500000000"));
		USER_LIST.put("1016", new BflowUserBo("1016", "王五", "拓展部-客服", "ext_kefu", "15533333333"));
		USER_LIST.put("1017", new BflowUserBo("1017", "赵七", "拓展部-客服", "ext_kefu", "15544444444"));
		USER_LIST.put("1018", new BflowUserBo("1018", "李浩", "拓展部-客服", "ext_kefu", "15555555555"));
	}

	private static void initRoleList(){
		ROLE_LIST.put("pm_manger", new BflowRoleBo("pm_manger",  "项目部-经理"));
		ROLE_LIST.put("pm_kefu", new BflowRoleBo("pm_kefu",  "项目部-客服"));

		ROLE_LIST.put("ext_manger", new BflowRoleBo("ext_manger",  "拓展部-经理"));
		ROLE_LIST.put("ext_kefu", new BflowRoleBo("ext_kefu",  "拓展部-客服"));
	}

	@Override
	public BflowUserBo getUser(String userId) {
		return USER_LIST.get(userId);
	}

	@Override
	public boolean hasRoleKey(String userId, String roleKey) {
		BflowUserBo userBo = USER_LIST.get(userId);
		return userBo==null?false:roleKey.equals(userBo.getRoleKey());
	}

	@Override
	public PageResult<BflowUserBo> findUserList(String filter) {
		PageResult<BflowUserBo> result = new PageResult<BflowUserBo>();
		result.setResult(new ArrayList(USER_LIST.values()));
		result.setTotalCount(USER_LIST.size());
		return result;
	}

	@Override
	public String getRoleName(String roleKey) {
		BflowRoleBo roleBo = ROLE_LIST.get(roleKey);
		return roleBo.getRoleName();
	}

	@Override
	public PageResult<BflowRoleBo> findRoleList(String filter) {
		PageResult<BflowRoleBo> result = new PageResult<BflowRoleBo>();
		result.setResult(new ArrayList(ROLE_LIST.values()));
		result.setTotalCount(ROLE_LIST.size());
		return result;
	}

	@Override
	public String getUserName(String userId) {
		if(StringUtils.isBlank(userId))
			return "";
		BflowUserBo userBo = USER_LIST.get(userId);
		if(userBo != null)
			return userBo.getUserName();
		return "";
	}

	@Override
	public String getUserNameOrRoleName(String key) {
		if(StrUtil.containsChinese(key))
			return key; //中文 直接返回
		String name = null;
		if(StrUtil.isEnglishWord(key))
			name = getRoleName(key);
		if(name == null)
			name = getUserName(key);
		return name;
	}


	@Override
	public String getUserRoleName(String userId) {
		if(StringUtils.isBlank(userId))
			return "";
		BflowUserBo userBo = getUser(userId);
		if(userBo != null)
			return userBo.getRoleName();
		return "";
	}
}
